package com.zhm.demo.utils.time;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class TimeIntervals {
    private List<TimeInterval> intervals;

    public TimeIntervals() {
        this.intervals = new ArrayList<>();
    }

    // 添加时间段，并检查是否重叠
    public boolean addInterval(TimeInterval interval) {
        for (TimeInterval existing : intervals) {
            if (existing.overlaps(interval)) {
                return false; // 发现重叠，不添加
            }
        }
        intervals.add(interval);
        return true; // 添加成功
    }

    // 获取所有时间段
    public List<TimeInterval> getIntervals() {
        return intervals;
    }

    // 打印所有时间段
    public void printIntervals() {
        for (TimeInterval interval : intervals) {
            System.out.println(interval);
        }
    }


    public static void main(String[] args) {
        TimeIntervals timeIntervals = new TimeIntervals();
        boolean result;

        result = timeIntervals.addInterval(new TimeInterval(LocalTime.of(9, 0), LocalTime.of(10, 0)));
        System.out.println("Added overlapping interval: " + result);
        result = timeIntervals.addInterval(new TimeInterval(LocalTime.of(10, 30), LocalTime.of(12, 0)));
        System.out.println("Added overlapping interval: " + result);
        result = timeIntervals.addInterval(new TimeInterval(LocalTime.of(9, 30), LocalTime.of(10, 15)));
        System.out.println("Added overlapping interval: " + result);

        timeIntervals.printIntervals();
    }

}
